package com.qing.search

import java.io.{File, IOException}
import java.nio.file.Paths

import org.apache.lucene.analysis.standard.StandardAnalyzer
import org.apache.lucene.document.{Document, Field, TextField}
import org.apache.lucene.index.{DirectoryReader, IndexWriter, IndexWriterConfig, Term}
import org.apache.lucene.search.{IndexSearcher, TermQuery}
import org.apache.lucene.store.FSDirectory

/**
  * Created by Administrator on 2017/8/26 0026.
  */
object TestLucene {
  def main(args: Array[String]): Unit = {
//    testCreateFile()

    search()
  }

  def testCreateFile(): Unit ={
    val file = new File("G:\\BigData\\data\\index\\lock")
    for (i <- 0 to 1000){
      println(file.createNewFile()+"....")
    }

  }

  private def search() = {
    val path = "/Users/wuliao/Desktop/lucene/"
    var directory:FSDirectory = null
    var count = 0
    try {
      var i = 0
      while ( {
        i < 10
      }) {
        val str = path + i
        if(new File(str).exists()){
          directory = FSDirectory.open(Paths.get(str))
          val analyzer = new StandardAnalyzer
//          val writerConfig = new IndexWriterConfig(analyzer)
//          val indexWriter = new IndexWriter(directory, writerConfig)
          val reader = DirectoryReader.open(directory)
          println(reader.numDocs()+"......")
//          val query = new TermQuery(new Term("port","80"))
//          val searcher = new IndexSearcher(reader)
//          count = count + searcher.count(query)
//          System.out.println(i + "..." + searcher.count(query))
//          indexWriter.commit
//          indexWriter.close()
        }
        {
          i += 1; i - 1
        }
      }
    } catch {
      case e: IOException =>
        e.printStackTrace()
    }
    System.out.println("。。。" + count)
  }


  private def index() = {
    val path = "/Users/wuliao/Desktop/index/"
    var directory:FSDirectory = null
    try {
      directory = FSDirectory.open(Paths.get(path))
      val analyzer = new StandardAnalyzer
      val writerConfig = new IndexWriterConfig(analyzer)
      val indexWriter = new IndexWriter(directory, writerConfig)
      val doc = new Document
      doc.add(new Field("name", "this is cname", TextField.TYPE_NOT_STORED))
      doc.add(new Field("content", "What is your name?my name is qing.Nice to meet you.", TextField.TYPE_STORED))
      indexWriter.addDocument(doc)
      indexWriter.commit
      indexWriter.close()
    } catch {
      case e: IOException =>
        e.printStackTrace()
    }
  }


}
